'use strict';

exports.main = async (event, context) => {

	switch (event.huichang) {
		case 'chuangxin':
			/* 
			融合分会场锁定10个登峰座位，20个炬火座位，10个其他
			个人成长分会场40个位子，不限身份
			其他两个60个位子，不限身份 
			 */
			let resres1 = tongyonga(event, context)

			return resres1

			break;
		case 'tuandui':
			let resres2 = tongyonga(event, context)
			return resres2
			break;
		case 'zhuanye': //40个座位,无权限限制
			let resres3 = tongyonga(event, context)
			return resres3
			break;
		case 'zhunru': //40个座位,无权限限制
			let resres4 = tongyonga(event, context)
			return resres4
			break;

		default:
			break;
	}

	async function tongyong(event, context) {
		const db = uniCloud.database()
		const dbCmd = db.command
		const coll = db.collection(event.huichang)
		const colluser = db.collection('user')


		let res1 = await coll.where({
			SeatCode: event.seatID
		}).get()
		console.log(res1)
		if (res1.data[0].Status == 0) {
			//更改座位状态
			let res2 = await coll.where({
				SeatCode: event.seatID
			}).update({
				Status: 2,
				suoding: 'suoding',
				userID: event.userInfo._id
			})
			console.log(res2)
			//更改选取人状态
			let res3 = await colluser.where({
				_id: event.userInfo._id
			}).update({
				status: dbCmd.inc(1)
			})
			let res4 = await colluser.where({
				_id: event.userInfo._id
			}).update({
				seat: dbCmd.push(event.seatID)
			})
			let result = Object.assign(res2, res3, res4)
			return result
		} else {
			return {
				"updated": 0,
				"msg": "该座位已被选择，请刷新重试"
			}
		}
	}

	async function tongyonga(event, context) {
		const db = uniCloud.database()
		const dbCmd = db.command
		const coll = db.collection(event.huichang)
		const colluser = db.collection('user')

		/* 1.校验该座是否被占用 2.没占用的话 先判断推荐人和会员本人的区域所在，同区+1  不同区+2 3.更新座位信息和个人信息 */
		let res1 = await coll.where({
			SeatCode: event.seatID
		}).get()
		if (res1.data[0].Status == 2) {
			return {
				"updated": 0,
				"msg": "该座位已被选择，请刷新重试"
			}
		} else {
			let res2 = await colluser.where({
				_id: event.userInfo
			}).get()
			if (res2.data[0].status == 1) {
				return {
					"updated": 0,
					"msg": "您已经选择过会场"
				}
			} else {
				//查询推荐人和本人是否同区
				if (event.tuijianid) {
					let res5 = await colluser.where({
						_id: event.tuijianid
					}).get()
					if (res5.data[0].area == res2.data[0].area) {
						let res6 = await colluser.where({
							_id: event.tuijianid
						}).update({
							jifen: dbCmd.inc(1),
						})
					} else {
						let res7 = await colluser.where({
							_id: event.tuijianid
						}).update({
							jifen: dbCmd.inc(2),
						})
					}
				}


				//更新个人信息


				let res3 = await colluser.where({
					_id: event.userInfo
				}).update({
					status: 1,
					seat: event.seatID,
					bianshou: event.bianshou
				})
				//更新座位
				let res4 = await coll.where({
					SeatCode: event.seatID
				}).update({
					Status: 2,
					suoding: 'suoding',
					userID: event.userInfo
				})
				return {
					"updated": 1,
					"seat": event.seatID
				}
			}
		}








		/* if (event.userInfo.group == 'dengfeng') {
			//先查看锁定座是否满了
			let res = await coll.where({suoding: 'dengfeng'}).count()
			if ( res.total < 10 ) {
				//锁定座位没满的话 拉取选中的座位 查看是否被选了
				let res1 = await coll.where({SeatCode: event.seatID}).get()
				// console.log(res1)
				if (res1.data[0].Status==0) {
					//更改座位状态
					let res2 = await coll.where({SeatCode: event.seatID}).update({
						Status:2,
						suoding:event.userInfo.group,
						userID:event.userInfo._id
					})
					console.log(res2)
					//更改选取人状态
					let res3 = await colluser.where({_id: event.userInfo._id}).update({
						status:dbCmd.inc(1)
					})
					let res4 = await colluser.where({_id: event.userInfo._id}).update({
						seat: dbCmd.push(event.seatID)
					})
					let result = Object.assign(res2,res3,res4)
					return result
				} else{
					return {"updated":0,"msg":"该座位已被选择，请刷新重试"}
				}
				
				
			} else{
				return {"updated":0,"msg":"座位名额已满，请选择其他分会场"} 
			}
			
			
		} else if(event.userInfo.group == 'juhuo'){ //聚火权限的
			let res = await coll.where({suoding: 'juhuo'}).count()
			if ( res.total < 20 ) {
				//锁定座位没满的话 拉取选中的座位 查看是否被选了
				let res1 = await coll.where({SeatCode: event.seatID}).get()
				// console.log(res1)
				if (res1.data[0].Status==0) {
					//更改座位状态
					let res2 = await coll.where({SeatCode: event.seatID}).update({
						Status:2,
						suoding:event.userInfo.group,
						userID:event.userInfo._id
					})
					console.log(res2)
					//更改选取人状态
					let res3 = await colluser.where({_id: event.userInfo._id}).update({
						status:dbCmd.inc(1)
					})
					let res4 = await colluser.where({_id: event.userInfo._id}).update({
						seat: dbCmd.push(event.seatID)
					})
					let result = Object.assign(res2,res3,res4)
					return result
				} else{
					return {"updated":0,"msg":"该座位已被选择，请刷新重试"}
				}
			} else{
				return {"updated":0,"msg":"座位名额已满，请选择其他分会场"} 
			}
			
			
		}else{
			//权限为none的
			let res = await coll.where({suoding: 'none'}).count()
			if ( res.total < 10 ) {
				//锁定座位没满的话 拉取选中的座位 查看是否被选了
				let res1 = await coll.where({SeatCode: event.seatID}).get()
				// console.log(res1)
				if (res1.data[0].Status==0) {
					//更改座位状态
					let res2 = await coll.where({SeatCode: event.seatID}).update({
						Status:2,
						suoding:event.userInfo.group,
						userID:event.userInfo._id
					})
					console.log(res2)
					//更改选取人状态
					let res3 = await colluser.where({_id: event.userInfo._id}).update({
						status:dbCmd.inc(1)
					})
					let res4 = await colluser.where({_id: event.userInfo._id}).update({
						seat: dbCmd.push(event.seatID)
					})
					let result = Object.assign(res2,res3,res4)
					return result
				} else{
					return {"updated":0,"msg":"该座位已被选择，请刷新重试"}
				}
			} else{
				return {"updated":0,"msg":"座位名额已满，请选择其他分会场"} 
			}
		} */
	}

};
